require(['config'], function () {
    require(['jquery'], function () {
        require(['jquery-color'], function () {
            $('#footer').load('footer.html .footer');
            function init() {
                //失去焦点，将手机号传给后端进行是否存在的匹配。后端返回匹配的结果。
                this.$phone = $('.input-phone');
                this.$pass = $('.input-key');
                this.$phonespan = $('span.phone');
                this.$pass2span = $('span.pass2');
                this.$phoyzmspan = $('span.phoyzm');
                this.$form = $('form');
                this.$checkbox = $('.checkbox');
                this.$btnreg = $('.btn-reg');

                let phonelock = false;
                let checklock = false;
                let passlock = false;
                let _this = this;
                //php接口公共地址
                const phpurl = 'http://10.31.155.44/02/yiguo/php/';
                // const phpurl = ' http://10.31.155.44/02/git/fkc/yiguo/php/';



                // 失焦时判断
                this.$phone.on('blur', function () {
                    let $phone = _this.$phone.val();
                    let reg = /^1\d{10}$/;

                    if ($phone) {
                        if (reg.test($phone)) {
                            $.ajax({
                                type: 'POST',
                                url: phpurl + 'register.php',
                                data: {
                                    phone: $phone
                                }
                            }).done(function (data) {
                                if (!data) {
                                    phonelock = true;
                                    _this.$phonespan.removeClass('pass-tip').removeClass('pass-error').addClass('pass-succ').html('<i class="tip"></i>该号码可注册');
                                } else {
                                    phonelock = false;
                                    _this.$phonespan.removeClass('pass-tip').removeClass('pass-succ').addClass('pass-error').html('<i class="tip"></i>该号码已注册');
                                }
                            });
                        } else {
                            phonelock = false;
                            _this.$phonespan.removeClass('pass-tip').removeClass('pass-succ').addClass('pass-error').html('<i class="tip"></i>手机号码格式有误');
                        }
                    } else {
                        phonelock = false;
                        _this.$phonespan.removeClass('pass-error').removeClass('pass-succ').addClass('pass-tip').html('<i class="tip"></i>手机号码不能为空');
                    }
                });

                this.$pass.eq(1).on('blur', function () {
                    let reg = /^.{6,20}$/;
                    let $pass1 = _this.$pass.eq(0).val();//第一次输入密码
                    let $pass2 = _this.$pass.eq(1).val();//重复输入的密码
                    if ($pass1 && $pass2) {
                        if (reg.test($pass1) && reg.test($pass2)) {
                            if ($pass1 === $pass2) {
                                passlock = true;
                                _this.$pass2span.removeClass('pass-tip').removeClass('pass-error').addClass('pass-succ').html('<i class="tip"></i>');
                            } else {
                                passlock = false;
                                _this.$pass2span.removeClass('pass-tip').removeClass('pass-succ').addClass('pass-error').html('<i class="tip"></i>两次密码不一致');
                            }
                        } else {
                            passlock = false;
                            _this.$pass2span.removeClass('pass-tip').removeClass('pass-succ').addClass('pass-error').html('<i class="tip"></i>密码格式不正确');
                        }
                    } else {
                        passlock = false;
                        _this.$pass2span.removeClass('pass-error').removeClass('pass-succ').addClass('pass-tip').html('<i class="tip"></i>密码不能为空');

                    }
                });

                //协议
                this.$checkbox.on('click', function () {
                    if ($(this).prop('checked')) {
                        _this.$btnreg.css({ "background": "#008842" });
                        checklock = true;
                    } else {
                        checklock = false;
                        _this.$btnreg.css({ "background": "grey" });
                    }
                });

                // 提交时判断
                this.$form.on('submit', function () {
                    if (!phonelock || !passlock || !checklock) {
                        return false;
                    }
                })
            };
            init();
        });
    });
});